<?php

class SmileyTag extends BaseOrm {
	public $EssentialColumns = array(
		't.id',
		't.sgenre_id',
		't.smiley_id'
		);
	public $TableName = 'msnu_smileysgenre';
	public $PrimaryColumn = 'id';
	public $Type = 'SmileyTag';

	public $_hasOne = array(
		'Tag' => 'sgenre_id',
		'Smiley' => 'smiley_id'
	);

	public function Id() {
		return $this->_values['id'];
	}
}

class SmileyTagList extends BaseOrmList {
	static function GetByTagId($tag_id, $offset = 0, $limit = NULL) {
		$smileytags = new SmileyTagList();

		$count_query = "
			SELECT
				COUNT(*) AS c
			FROM
				msnu_smileysgenre AS f
			WHERE
				f.sgenre_id=" . $tag_id . "
			";

		$query = "
			SELECT
				" . SmileyTag::$EssentialColumns . "
			FROM
				msnu_smileyfavorite AS f,
				msnu_usr AS u,
				msnu_smiley AS s
			WHERE
				f.usr_id=" . $user_id . " AND
				f.smiley_id=s.id AND
				u.id=s.usr_id AND
				u.enabled=1 AND
				s.enabled=1 AND
				f.enabled=1
			ORDER BY
				sequence
					" . (($limit)?"
					LIMIT
						" . intval($offset) . ", " . intval($limit):"");

		$count_result = Dbi::GetResult($count_query);

		if ( (!$row_count = mysql_fetch_assoc($count_result)) || !intval($row_count['c']) ) return NULL;
		$smileys->TotalCount = $row_count['c'];

		$result = Dbi::GetResult($query);

		while ( $row = mysql_fetch_assoc($result) ) {
			$smiley = new Smiley($row);
			$smileys->Values[] = $smiley;
		}

		return $smileys;
	}
}